/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/10/3 20:22
 * 设计循环队列
 */

class MyCircularQueue {
    public int[] elem;
    public int front;//对头下标
    public int rear;//队尾下标
    public MyCircularQueue(int k) {
        this.elem = new int[k+1];

    }

    /**
     * 入队操作
     * @param value
     * @return
     */
    public boolean enQueue(int value) {
        if(isFull()) return false;
        this.elem[rear] = value;
        rear = (rear+1)% elem.length;
        return true;
    }

    /**
     * 出队
     * @return
     */
    public boolean deQueue() {
       if(isEmpty()) return false;
       front = (front+1)% elem.length;
       return true;
    }

    /**
     * 得到队头元素
     * @return
     */
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int index = -1;
        /**
         * 防止越界
         */
        if(rear == 0){
            index = elem.length-1;
        }else{
           index = rear-1;
        }
        return elem[index];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    /**
     * 判断循环队列是否满
     * @return
     */
    public boolean isFull() {
        if((rear+1)% elem.length == front){
            return true;
        }
        return false;
    }
}


